package cn.source.web.controller.db;

import cn.hutool.core.date.DateUtil;
import cn.source.common.core.domain.AjaxResult;
import cn.source.zk.domain.dto.DbDataAnalysisQueryDTO;
import cn.source.zk.domain.entity.Channel;
import cn.source.zk.service.IDbAnalysisDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.LinkedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/db/dataAnalysis")
@Slf4j
public class DbDataAnalysisController {

    @Autowired
    private IDbAnalysisDataService analysisDataService;

    /**
     * 折线图数据
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public AjaxResult flowLine(@RequestBody DbDataAnalysisQueryDTO dto) {
        log.info("传递的参数：{}", dto);

        Map<String, Object> map = analysisDataService.flowLine(dto);
        return AjaxResult.success(map);
    }

    @PostMapping("/getChannelList")
    public AjaxResult getChannelList(@RequestBody DbDataAnalysisQueryDTO dto) {
        List<Channel> list = analysisDataService.getChannelList(dto);
        return AjaxResult.success(list);
    }

    @GetMapping("/dataBase")
    public AjaxResult dataBase(DbDataAnalysisQueryDTO dto,@RequestParam("columns") String columns) {
        List<Map<String, Object>> database = analysisDataService.getFlowDatabase(dto,columns);
        Map<String, Object> reuslt = new LinkedHashMap<>();
        String[] strings = columns.split(",");
        List<String> timeDateList = new ArrayList<String>();
        for (String strkey : strings) {
            List<Object> arrayList = new ArrayList<>();
            for (Map<String, Object> map : database) {
                arrayList.add(map.get(strkey));

                if(strkey.equals(strings[0])){
                    LocalDateTime timedate = (LocalDateTime) map.get("timedate");
                    timeDateList.add(DateUtil.format(timedate, "yyyy-MM-dd HH:mm:ss"));
                }
            }
            reuslt.put(strkey, arrayList);
        }
        reuslt.put("timeDate", timeDateList);


        return AjaxResult.success(reuslt);
    }

}
